package yl.hs.bmipfull.service.host.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.host.IProductionLineCodeConfigDao;
import yl.hs.bmipfull.daos.host.IProductionLineRawConfigDao;
import yl.hs.bmipfull.daos.host.IProductionLineSqlConfigDao;
import yl.hs.bmipfull.pojo.host.ProductionLineCodeConfigEntity;
import yl.hs.bmipfull.pojo.host.ProductionLineRawConfigEntity;
import yl.hs.bmipfull.pojo.host.ProductionLineSqlConfigEntity;
import yl.hs.bmipfull.service.host.IProductionLineCodeConfigService;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:  生产线主机字段设置服务层接口实现<br/>
 * 创建时间: 2021年06月28日 14时34分32秒 <br/>
 *
 * @author 谢东
 */
@Service
public class ProductionLineCodeConfigServiceImpl extends SystemBaseService implements IProductionLineCodeConfigService {

    @Autowired
    IProductionLineCodeConfigDao dao;

    @Autowired
    IProductionLineSqlConfigDao sqlDao;
    @Autowired
    IProductionLineRawConfigDao rawDao;



    @Override
    public List<ProductionLineCodeConfigEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductionLineCodeConfigEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<ProductionLineCodeConfigEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductionLineCodeConfigEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<ProductionLineCodeConfigEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public Map<String, Object> findByLineSn(String sn) {
        Map<String, Object> map = new HashMap<>();
        map.put("CodeConfigModel", dao.findBySN(sn));
        map.put("SqlConfigModel", sqlDao.findBySN(sn));
        map.put("RawConfigList", rawDao.getListByPlSN(sn));
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveProductionLineCodeConfig(Map<String, Object> codeModel, Map<String, Object> sqlModel,
                                                List<ProductionLineRawConfigEntity> rawConfigList)
            throws Exception {
        Integer result = 0;
        ProductionLineCodeConfigEntity entity = new ProductionLineCodeConfigEntity();
        Tl.getChangeLog(entity, codeModel);
        entity.setPYFieldValue();
        dao.deleteByPlSN(entity.getPlSN());
        result = dao.insertNew(entity);
        if (result > 0) {
            ProductionLineSqlConfigEntity sqlEntity = new ProductionLineSqlConfigEntity();
            Tl.getChangeLog(sqlEntity, sqlModel);
            sqlEntity.setPYFieldValue();
            sqlDao.deleteByPlSN(sqlEntity.getPlSN());
            sqlDao.insertNew(sqlEntity);
            rawDao.deleteByPlSN(rawConfigList.get(0).getPlSn());
            for (ProductionLineRawConfigEntity rawEntity : rawConfigList) {
                rawEntity.setPYFieldValue();
                rawDao.insertNew(rawEntity);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {

        return 0;
    }
}